﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace FCNS.Controls
{
    public partial class DigitTextBox : System.Windows.Forms.TextBox
    {
        public DigitTextBox()
        {
            InitializeComponent();
        }
      
        private Color ColorPlus = Color.Black;
        /// <summary>
        /// 正数字体颜色
        /// </summary>
        [Category("FCNS"), Description("正数字符的颜色"), DefaultValue("Black")]
        public Color PlusColor
        {
            get
            {
                return ColorPlus;
            }
            set
            {
                ColorPlus = value;
            }
        }
        
        private Color ColorNegative = Color.Red;
        /// <summary>
        /// 负数字体颜色
        /// </summary>
        [Category("FCNS"), Description("负数字符的颜色"), DefaultValue("Red")]
        public Color NegativeColor
        {
            get
            {
                return ColorNegative;
            }
            set
            {
                ColorNegative = value;
            }
        }

        /// <summary>
        /// 文本转换成数值
        /// </summary>
        [Category("FCNS"), Description("文本转换成数值")]
        public int Value
        {
            get
            {
                if (this.Text == "")
                {
                    return 0;
                }
                else
                {
                    return int.Parse(this.Text);
                }
            }
            set
            {
                this.Text = value.ToString();
            }
        }
        //string text = "0";
        //public override string Text
        //{
        //    get
        //    {
        //        return text;
        //    }
        //    set
        //    {
        //        if (Regex.IsMatch(value, @"^[-]?\d+(\.\d+)?$"))
        //        {
        //            text = value;
        //        }
        //        else
        //        {
        //            MessageBox.Show("超出字符长度限制");
        //        }
        //    }
        //}

        bool cnDigit = false;
        /// <summary>
        /// 显示中文数字格式
        /// </summary>
        //[Category("FCNS"), Description("显示中文数字格式"), DefaultValue("False")]
        //public bool CNdigit
        //{
        //    get
        //    {
        //        return cnDigit;
        //    }
        //    set
        //    {
        //        cnDigit = value;
        //        this.Text = ChangeDigitWord(value);
        //    }
        //}

        bool printBorder = false;
        /// <summary>
        /// 是否打印边框
        /// </summary>
        [Category("FCNS"), Description("是否打印边框"), DefaultValue("False")]
        public bool PrintBorder
        {
            get
            {
                return printBorder;
            }
            set
            {
                printBorder = value;
            }
        }

        string columnText = "无";
        /// <summary>
        /// 绑定数据表中的列头文本
        /// </summary>
        [Category("FCNS"), Description("绑定数据表中的列头文本"), DefaultValue("无")]
        public string DataColumnText
        {
            get
            {
                return columnText;
            }
            set
            {
                columnText = value;
            }
        }

        int maxLength = 10;
        public override int MaxLength
        {
            get
            {
                return maxLength;
            }
            set
            {
                if (value < 11)
                {
                    maxLength = value;
                }
                else
                {
                    MessageBox.Show("Int32 长度最大为 10");
                }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 8)
            {
            }
            else if (this.Text.Length == maxLength)
            {
                e.Handled = true;
                MessageBox.Show("超出字符长度限制");
            }
            else if (char.IsDigit(e.KeyChar))
            {
            }
            //else if (e.KeyChar == '-' && !this.Text.Contains("-"))
            //{
            //    this.Text.Insert(0, "-");
            //}
            else if ((e.KeyChar == '-' && this.Text.Length == 0) | this.SelectedText == this.Text)
            {

            }
            else if (e.KeyChar == '.')
            {
                if (this.Text.Split('.').Length > 1 | this.Text == "" | this.Text == "-")
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }

        protected override void OnLeave(EventArgs e)
        {
            if (this.Text.EndsWith("."))
            {
                this.Text = this.Text.Remove(this.Text.Length - 1);
            }
            if (this.Text.EndsWith("-"))
            {
                this.Text = "";
            }

            if (this.Text.StartsWith("-"))
            {
                this.ForeColor = NegativeColor;
            }
            else
            {
                this.ForeColor = PlusColor;
            }
            base.OnLeave(e);
        }
    }
}